//
//  WoDeAcountVC.m
//  SanYiTeaching
//
//  Created by 李文强 on 2019/4/27.
//  Copyright © 2019 SanYi. All rights reserved.
//

#import "WoDeAcountVC.h"
#import "SYRequestAPI+Wode.h"
#import "WodeInforModel.h"

@interface WoDeAcountVC ()<WoDeNavViewDelegate,UITextFieldDelegate>{
    BOOL isEdite;
    UITextField *name_new;//姓名
    UITextField *phone_new;//手机号——新
    UITextField *yanZhengMa_new;//验证码
}
@property (nonatomic, strong) WodeInforModel *model;
@end

@implementation WoDeAcountVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}
- (void)initSubView{
    _model = [WodeInforModel new];
    isEdite = NO;
    self.view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.navView];
    [self.navView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@0);
        make.right.equalTo(@0);
        make.top.equalTo(@0);
        make.height.equalTo(@50);
    }];
    
    [self.view addSubview:self.tableList];
    [_tableList mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@100);
        make.right.equalTo(@0).offset(-100);
        make.top.equalTo(self->_navView.mas_bottom).offset(30);
        make.height.equalTo(@100);
    }];
    
    [self getAcountInforRequest];
}
- (WoDeNavView *)navView{
    if (!_navView) {
        WoDeNavView *topView = [[WoDeNavView alloc]init];
        topView.frame = CGRectMake(0, 0, self.view.frame.size.width,50);
        topView.delegate = self;
        _navView = topView;
    }
    return _navView;
}
- (void)alertView{

    isEdite = !isEdite;
    [self.tableList reloadData];
}
- (UITableView *)tableList{
    if (!_tableList) {
        UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 144, self.view.frame.size.width-200, APP_HEIGHT-84) style:UITableViewStylePlain];
        tableView.delegate  =self;
        tableView.dataSource = self;
        tableView.backgroundColor = [UIColor whiteColor];
        [tableView registerClass:[WoDeAcountCell class] forCellReuseIdentifier:@"WoDeAcountCell"];
        tableView.layer.borderColor = LINCOLOR.CGColor;
        tableView.layer.borderWidth = 1;
        tableView.scrollEnabled = NO;
        tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableList = tableView;
    }
    return _tableList;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 2;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    WoDeAcountCell *cell = [tableView dequeueReusableCellWithIdentifier:@"WoDeAcountCell" forIndexPath:indexPath];
    if (!cell) {
        cell = [[WoDeAcountCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"WoDeAcountCell"];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (indexPath.row == 0) {
        cell.titleLabel.text = @"姓名";
        cell.tf.text = _model.name;
        cell.tf.tag = 1001;
        name_new = cell.tf;
        cell.tf.delegate = self;
        [cell.editeButton addTarget:self action:@selector(editeClick:) forControlEvents:UIControlEventTouchUpInside];
    }else{
        cell.titleLabel.text = @"手机号";
        cell.tf.text = _model.mobile;
        cell.tf.delegate = self;
        cell.tf.tag = 1000;
        phone_new = cell.tf;
        [cell.editeButton addTarget:self action:@selector(phoneEditeClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    cell.userInteractionEnabled = isEdite;
    if (isEdite == NO) {
        [cell closeViewLayout];
    }else{
        if (indexPath.row == 0) {
            [cell.tf becomeFirstResponder];
        }
        [cell reloadViewLayout];
    }
    cell.selected = NO;
    return cell;
}
- (void)editeClick:(UIButton *)sender{
    //更改姓名
}
- (void)phoneEditeClick:(UIButton *)sender{
    //更改手机号
        [UIApplication sharedApplication].keyWindow.rootViewController.definesPresentationContext = YES;
        WoDeEditeAlertView *view = [[WoDeEditeAlertView alloc]init];
    view.enterView.enterTF.text = phone_new.text;
    yanZhengMa_new = view.yanZhengMaView.enterTF;
        view.modalPresentationStyle = UIModalPresentationOverCurrentContext;
        view.view.superview.frame = CGRectMake(0, 0,self.view.frame.size.width, APP_HEIGHT);
        view.cancelBlock = ^{
            [view dismissViewControllerAnimated:NO completion:nil];
            [self.view endEditing:YES];
        };
        view.confirmlBlock = ^{
            [self uploadPhoneNumberRequest];
            [self.view endEditing:YES];
        };
        [self presentViewController:view animated:NO completion:nil];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"点击了cell");
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view = [[UIView alloc]init];
    return view;
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}
#pragma mark - 得到账户信息
- (void)getAcountInforRequest{
    NSDictionary *parama = @{};
    [SYRequestAPI sy_getAccountServiceAccountInfo:parama success:^(id  _Nonnull responseObject) {
        NSLog(@"我的-账户信息-%@",responseObject);
        NSDictionary *dic = [responseObject objectForKey:@"data"];
        self->_model = [WodeInforModel yy_modelWithJSON:dic];
        [self.tableList reloadData];
    } fail:^(NSError *error) {
        
    }];
}
//
//#pragma mark - 手机号姓名修改代理
//- (void)textFieldDidEndEditing:(UITextField *)textField{
//    if (textField.tag == 1000) {
//        //手机号
//        phone_new = textField.text;
//    }else{
//        //姓名
//        name_new = textField.text;
//    }
//}
//#pragma mark - 更新手机号
- (void)uploadPhoneNumberRequest{
    if (yanZhengMa_new.text.length == 0) {
        [XSInfoView showInfo:@"请输入验证码" onSubView:self.view];
        return;
    }
    if ([PublickFunctionObject checkPhone:phone_new.text]) {
        NSDictionary *parama = @{@"validKey":yanZhengMa_new.text,
                                 @"newMobile":phone_new.text};
        [MBProgressHUD mj_showMessage:nil toView:self.view];
        [SYRequestAPI sy_getSafeServiceChangeMobile:parama success:^(id  _Nonnull responseObject) {
            NSLog(@"更新手机号成功 %@",responseObject);
            [MBProgressHUD mj_hideHUDForView:self.view];
        } fail:^(NSError *error) {
            [MBProgressHUD mj_hideHUDForView:self.view];
        }];
    }else{
        [XSInfoView showInfo:@"手机号不正确" onSubView:self.view];
    }
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
